package cn.chencaiju.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * web层：请求参数封装、请求路径映射
 * spring mvc (model, view, controller)
 */
@Controller
public class ParamController {

	@RequestMapping("/param/request")
	public String request(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		// 从HtttServletRequest里面取请求参数
		String id = request.getParameter("id");
		System.out.println("id:" + id);
		
		System.out.println("response:" + response);
		System.out.println("session:" + session);

		return "success";
	}
	
	// model 是用来给视图层传数据的
	@RequestMapping("/param/model")
	public String model(HttpSession session, Model model) {
		
		model.addAttribute("user", 17);
		
		session.setAttribute("spring", "农夫山泉");
		
		return "success";
	}

	// 如果形参的名字和请求参数的key不对应，使用@RequestParam来对应
	// /param/parameter?myparam=abc
	@RequestMapping("/param/parameter")
	public String parameter(@RequestParam("myparam") String myparam) {
		System.out.println(myparam);
		
		return "success";
	}	
}
